package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.User;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.RedisUtils;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsShipmentOrder;
import com.jy.wms.pojo.WmsShipmentOrderDetailed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.jy.wms.pojo.WmsDistribution;
import com.jy.wms.service.WmsDistributionService;
import javax.servlet.http.HttpSession;
import com.github.pagehelper.Page;

import java.util.ArrayList;
import java.util.List;
import com.jy.wms.common.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @ClassName: WmsDistributionController
 * @Description:发运订单-分配
 * @Author yjs
 * @Date Tue Oct 24 13:54:01 GMT+08:00 2017
 */
@Controller
@RequestMapping("/wmsdistribution")
@Api(value = "/wmsdistribution", description = "关于发运订单-分配操作")
public class  WmsDistributionController extends BaseController<WmsDistributionService,WmsDistribution> {
    @Autowired
    RedisUtils redisUtils;
    /**
     * 分配管理
     * @param setAndModifyModel
     * @param session
     * @return
     */
    @PostMapping(value = "distribution")
    @ApiOperation(value = "分配管理 POST分配操作",response = JSONObject.class, notes = "POST分配操作(拣货状态为'初始',发运状态为'初始',审核状态为'已审核',拆单状态为'已拆单'的发运订单)")
    public @ResponseBody JSONObject distribution(@RequestBody SetAndModifyModel setAndModifyModel, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //取当前货主编号
            String hzId=null;
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                hzId=hz.get(0).getHzid();
                baseService.distribution(setAndModifyModel.getIds(),userinfo,hzId);
            }
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "分配操作成功", "", "");
        }catch (RuntimeException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"分配操作失败",e.getMessage(),"");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"分配操作失败",e.getMessage(),"");
        }catch (Exception e) {
            for(String id:setAndModifyModel.getIds()){
                redisUtils.remove("分配"+id);
            }
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"分配操作失败","分配操作失败","");
        }
        return json;
    }

    /**
     * 取消分配管理
     * @param setAndModifyModel
     * @param session
     * @return
     */
    @PostMapping(value = "cancelDistribution")
    @ApiOperation(value = "取消分配管理 POST取消分配操作", response = JSONObject.class, notes = "POST取消分配操作(拣货状态为'初始',装箱状态为'未装箱'的发运订单)")
    public @ResponseBody JSONObject cancelDistribution(@RequestBody SetAndModifyModel setAndModifyModel, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //取当前货主编号
            String hzid=null;
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                hzid=hz.get(0).getHzid();
                baseService.cancelDistribution(setAndModifyModel.getIds(),userinfo,hzid);
            }
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "取消分配操作成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"取消分配操作失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"取消分配操作失败","取消分配操作失败","");
        }
        return json;
    }

    /**
     * 追加分配管理
     * @param setAndModifyModel
     * @param session
     * @return
     */
    @PostMapping(value = "addDistribution")
    @ApiOperation(value = "追加分配管理 POST追加分配操作",response = JSONObject.class, notes = "POST追加分配操作(发运状态为'部分分配',审核状态为'已审核',拆单状态为'已拆单'的发运订单)")
    public @ResponseBody JSONObject addDistribution(@RequestBody SetAndModifyModel setAndModifyModel, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //取当前货主编号
            String hzid=null;
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                hzid=hz.get(0).getHzid();
                baseService.addDistribution(setAndModifyModel.getIds(),userinfo,hzid);
            }
        }catch (SucessException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "追加分配操作成功", "", "");
        }catch (ServiceException e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"追加分配操作失败",e.getMessage(),"");
        }catch (Exception e) {
            for(String id:setAndModifyModel.getIds()){
                redisUtils.remove("追加分配"+id);
            }
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"追加分配操作失败","追加分配操作失败","");
        }
        return json;
    }
    /**
     * 查看发运订单明细
     * @param wmsShipmentOrderDetailed
     * 传发运订单ID,用发运订单明细实体类接收,放在发运订单明细实体类ID内
     * @return
     */
    @PostMapping(value = "searchDetailed")
    @ApiOperation(value = "查看发运订单明细 POST查看发运订单明细操作",response = JSONObject.class, notes = "POST查看发运订单明细")
    public @ResponseBody JSONObject searchDetailed(@RequestBody WmsShipmentOrderDetailed wmsShipmentOrderDetailed) {
        JSONObject json = new JSONObject();
        Page<WmsShipmentOrderDetailed> list = new Page<WmsShipmentOrderDetailed>();
        try {
            json=baseService.searchDetailed(wmsShipmentOrderDetailed);
            //json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (ServiceException e){
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"查看发运订单明细失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看发运订单明细失败","查看发运订单明细失败","");
        }
        return json;
    }
    /**
     * 查看分配明细
     * @param wmsDistribution
     * 传发运订单明细ID,用分配实体类接收,放在分配实体类ID内
     * @return
     */
    @PostMapping(value = "distributionDetailed")
    @ApiOperation(value = "查看分配明细 POST查看分配明细操作",response = JSONObject.class, notes = "POST查看分配明细")
    public @ResponseBody JSONObject distributionDetailed(@RequestBody WmsDistribution wmsDistribution) {
        JSONObject json = new JSONObject();
        List<WmsDistribution> list = new ArrayList<>();
        try {
            json=baseService.distributionDetailed(wmsDistribution);
            //json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),list.size());
        }catch (ServiceException e){
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.warning.toString(),"查看分配明细失败",e.getMessage(),"");
        }catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查看分配明细失败","分配明细失败","");
        }
        return json;
    }

    /**
     * 查看分配明细(人工修改分配记录专用)
     *
     * @param wmsDistribution
     * @return
     */
    @PostMapping(value = "searchDistributionDetailed")
    @ApiOperation(value = "修改分配明细记录 POST查看分配明细记录操作", response = JSONObject.class, notes = "POST查看分配明细记录")
    public @ResponseBody
    JSONObject searchDetailed(@RequestBody WmsDistribution wmsDistribution, HttpSession session) {
        JSONObject json = new JSONObject();
        Page<WmsDistribution> list = new Page<WmsDistribution>();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            wmsDistribution.setCkid(ckid);
            list = baseService.searchDistributionDetailed(wmsDistribution);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 修改分配明细(分配数量只能减少,不能增加)
     *
     * @param wmsDistribution
     * @return
     */
    @PostMapping(value = "updateDistributionDetailed")
    @ApiOperation(value = "修改分配明细 POST修改分配明细操作", response = JSONObject.class, notes = "POST修改分配明细")
    public @ResponseBody
    JSONObject updateDistributionDetailed(@RequestBody WmsDistribution wmsDistribution, HttpSession session) {
        JSONObject json = new JSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //取当前货主编号
            String hzid = null;
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            baseService.updateDistributionDetailed(wmsDistribution, hzid);
        } catch (SucessException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "分配操作成功", "", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "分配操作失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "分配操作失败", "分配操作失败", "");
        }
        return json;
    }
}
